package controllers.roleAndPermeission;

import com.fasterxml.jackson.databind.node.ObjectNode;
import models.roleAndPermessions.Permeission;
import models.roleAndPermessions.Role;
import models.roleAndPermessions.RoleAndPermeission;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.permessionManager.authoManager;

import java.util.ArrayList;
import java.util.List;

/**
 * This controller contains an action to handle HTTP requests
 * to the application's home page.
 */
public class RoleAndPermeissionController extends Controller {
    /**
     * 加载权限页面
     * @return
     */
    public static Result loadPerPage() {
        return ok(authoManager.render("JqueryuiDatepicker"));
    }
    public static Result findAllPermeissons(){
        ObjectNode resultNode = Json.newObject();
        List<Permeission> permeissionList =Permeission.findAll();
        resultNode.put("perssions",Json.toJson(permeissionList));
        System.out.println(Json.toJson(permeissionList));
        return  ok(Json.toJson(permeissionList).toString());
    }
    public static Result findAllRoles(){
        ObjectNode resultNode = Json.newObject();
        List<Role> rolesList = Role.findAll();
        resultNode.put("roles",Json.toJson(rolesList));
        System.out.println(Json.toJson(rolesList));
        return  ok(Json.toJson(rolesList).toString());
    }

    /**
     * 转换权限数据为json格式
     * @return
     */
    public static Result changeListToTree(){
        List<List<Permeission>> list =Permeission.findEveLevelL();
        List sourceList = new ArrayList();
        int size =list.size();
        if (size>=1){
            List<Permeission> rootNodes =  list.get(0);//根节点
                for (Permeission onePer:rootNodes){
                    int oneGrade =onePer.grade;
                    if (oneGrade<size){//说明不是等级最低的子节点，否则是
                        sourceAddChild(list,onePer);
                    }
                    sourceList.add(onePer);
                }
        }
        return  ok(Json.toJson(sourceList).toString());
    }

    /**
     * 考察知识点：引用类型实际参数的变化影响形式参数，因为传递的是地址值，所以此处返回值可以为void
     * @param size   权限等级list的长度
     * @param lastId  父节点的id
     * @param grade  权限等级
     * @param list    权限等级list
     * @param lastPer 节点对象
     */
    public static void sourceAddChild(List<List<Permeission>> list,Permeission lastPer){
        int size = list.size();
        int grade =lastPer.grade;
        if (grade<size){
            List<Permeission> nextNodes =  list.get(grade);
            for (Permeission nextPer:nextNodes){
                String nextParent = nextPer.parent;
                if (lastPer.id.equals(nextParent)){
                        sourceAddChild(list,nextPer);//调用自身方法添加节点下的子节点
                    lastPer.children.add(nextPer);
                }
            }
        }

    }

    public static Result permeissonsOfRole(Integer roleId){
        System.out.println(roleId);

        List<String> allSonPermeissionOfRole =RoleAndPermeission.findPermeissionOfRole(roleId);
            return ok(Json.toJson(allSonPermeissionOfRole).toString());
    }
}
